package com.lagou.edu.controller;

import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.service.impl.LagouUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * 邮件控制器
 */
@RestController
@RequestMapping({"/user",""})
@RefreshScope
public class UserController {

    @Autowired
    LagouUserServiceImpl lagouUserService;

    @GetMapping("register/{email}/{password}/{code}")
    public int register(@PathVariable("email") String email,
                            @PathVariable("password") String password,
                            @PathVariable("code") String code){
        return lagouUserService.register(email,password,code);
    }

    @GetMapping("isRegistered/{email}")
    public boolean isRegistered(@PathVariable("email") String email){
        return lagouUserService.isRegistered(email);
    }

    @GetMapping("login/{email}/{password}")
    public String login(
            @PathVariable("email") String email,
            @PathVariable("password") String password,
            HttpServletResponse response){
        LagouToken lagouToken = lagouUserService.login(email, password);
        if(lagouToken==null){
            return "false";
        }
        Cookie cookie = new Cookie("token", lagouToken.getToken());
        cookie.setPath("/");
        response.addCookie(cookie);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return email;
    }

    @GetMapping("info/{token}")
    public String info(@PathVariable("token") String token){
        return lagouUserService.info(token);
    }



}
